package fr.upmc.i3s.fuglmeyeranalyser.view.menus;

import java.awt.GridLayout;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.AbstractMCOSolution;
import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.AbstractMCPSolution;
import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.AbstractMCPkSolution;
import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.AbstractSolution;
import fr.upmc.i3s.fuglmeyeranalyser.models.solutions.SolutionList;
import fr.upmc.i3s.fuglmeyeranalyser.patients.PatientParameter;
import fr.upmc.i3s.fuglmeyeranalyser.view.strings.StringsConstants;

public class MenuDataOut extends JPanel {

	private static final long serialVersionUID = -5852480518548793350L;
	
	private Object[][] datas;

	/**
	 * Create the panel.
	 * @param paramsList 
	 */
	public MenuDataOut(SolutionList solutions, LinkedList<String> paramsList) {
		super(new GridLayout(0, 1));
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
		JTable table = createTable(solutions, paramsList);
		JScrollPane scrollpane = new JScrollPane();
		scrollpane.setViewportView(table);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		add(scrollpane);
	}
	
	public JTable createTable(SolutionList solutions, LinkedList<String> paramsList) {
		int optionalParamsNb = paramsList.size();
		int solutionsNumber = 3;
		int patientNb = solutions.size();
		System.out.println("patientNb="+patientNb+", optionalParamsNb="+optionalParamsNb+", solutionsNumber="+solutionsNumber);
		
		// build entetes
		String[] entetes = new String[optionalParamsNb+solutionsNumber]; 
		int cpt = 0;
		for(String param : paramsList) {
			entetes[cpt] = param;
			cpt++;
		}
		entetes[cpt] = StringsConstants.ROW_NAME_FM0;
		entetes[cpt+1] = StringsConstants.ROW_NAME_BETA;
		entetes[cpt+2] = StringsConstants.ROW_NAME_KAPA;
		
		// build data matrix
		datas = new Object[patientNb][optionalParamsNb+solutionsNumber];
		int i = 0;
		for(AbstractSolution solution : solutions) {
			cpt = 0;
			Object[] solutionData = new Object[optionalParamsNb+solutionsNumber];
			LinkedList<PatientParameter> patientParams = solution.getPatient().getParamsList();
			for(PatientParameter patientParam : patientParams) {
				solutionData[cpt] = patientParam.getValue();
				cpt++;
			}
			
			if(solution instanceof AbstractMCOSolution) {
				AbstractMCOSolution sol = (AbstractMCOSolution) solution;
				solutionData[cpt] = sol.getFm0();
				solutionData[cpt+1] = sol.getBeta();
				solutionData[cpt+2] = "-";
				
			} else if(solution instanceof AbstractMCPSolution) {
				AbstractMCPSolution sol = (AbstractMCPSolution) solution;
				solutionData[cpt] = sol.getFm0();
				solutionData[cpt+1] = sol.getBeta();
				solutionData[cpt+2] = "-";
				
			} else if(solution instanceof AbstractMCPkSolution) {
				AbstractMCPkSolution sol = (AbstractMCPkSolution) solution;
				solutionData[cpt] = sol.getFm0();
				solutionData[cpt+1] = sol.getBeta();
				solutionData[cpt+2] = sol.getKapa();
			}
			
			datas[i] = solutionData;
			i++;
		}
		
		// build table
		JTable table = new JTable(datas, entetes);
		table.getTableHeader().setReorderingAllowed(false);
		return table;
	}
	
	public void setData(Object[][] donnees) {
		this.datas = donnees;
	}

}
